package com.yunmang.adapter.setting;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.yunmang.R;
import com.yunmang.utils.SharedPreferencesUtils;

import java.util.List;
import java.util.Map;

public class PrintAdapter extends BaseAdapter {
	private List<Map<String,String>> list;
	private LayoutInflater inflater;
	private static String mac = "";
	private static int changeColorPostion = -1;
	private Context context;
	public PrintAdapter(Context context, List<Map<String,String>> list) {
		this.list = list;
		inflater = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Map<String,String> map = list.get(position);
		ViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.activity_set_print_item, null);
			viewHolder.deviceNameTv = (TextView) convertView.findViewById(R.id.set_print_DeviceName);
			viewHolder.bdAddressTv = (TextView) convertView.findViewById(R.id.set_print_BDAddress);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(changeColorPostion>-1){
			if (changeColorPostion == position) {
				convertView.setBackgroundColor(Color.BLUE);
			}else{
				convertView.setBackgroundColor(Color.WHITE);
			}
		}else{
			if(map.get("BDAddress").equals(SharedPreferencesUtils.getParameter(context, "SelectedPrintMac"))){
				convertView.setBackgroundColor(Color.BLUE);
			}else{
				convertView.setBackgroundColor(Color.WHITE);
			}
		}
			
		viewHolder.deviceNameTv.setText(map.get("DeviceName"));
		viewHolder.bdAddressTv.setText(map.get("BDAddress"));
		return convertView;
	}

	public static class ViewHolder {
		public TextView deviceNameTv;// 打印机型号
		public TextView bdAddressTv;// 打印机
	}
	public static void changeColor(int position) {
		PrintAdapter.changeColorPostion = position;
	}
	
}
